var log = console.log.bind(console)
var e = sel => document.querySelector(sel)

// log = function(s) {
//     e('#id-text-log').value = '\n' + String(s)
// }
var config = {
    player_speed: 10,
    cloud_speed: 10,
    bullet_speed: 10,
    cool_down: 4,

    screen_width: 1000,
    screen_height: 500,

    // enemy
    coolDownToDie: 40,
}

var rectIntersect = function(o, b) {
    // 碰撞算法，简单点，判断一个矩形的四个点是否在另一个矩形中
    var a = o
    var aLeft = a.x
    var bLeft = b.x
    var aRight = a.x + a.image.width
    var bRight = b.x + b.image.width
    var aUp = a.y
    var bUp = b.y
    var aDown = a.y + a.image.height
    var bDown = b.y + b.image.height

    if ((aLeft >= bLeft && aLeft < bRight) && (aUp >= bUp && aUp < bDown)) {
        return true
    }
    if ((aRight >= bLeft && aRight < bRight) && (aUp >= bUp && aUp < bDown)) {
        return true
    }
    if ((aLeft >= bLeft && aLeft < bRight) && (aDown >= bUp && aDown < bDown)) {
        return true
    }
    if ((aRight >= bLeft && aRight < bRight) && (aDown >= bUp && aDown < bDown)) {
        return true
    }
    return false
}

var imageFromPath = function(path) {
    var img = new Image()
    img.src = path
    return img
}


var pointInItem = function(x, y, item) {
    xIN = x > item.x && x < item.x + item.w
    yIN = y > item.y && y < item.y + item.h
        // log('pointItem', x, y, item.x, item.y, item.w, item.h)
        // log('xIN && yIN', xIN && yIN)
    return xIN && yIN
}

var ajax = function(method, path, data, responseCallback) {
    log('ajax request', method, path, data, responseCallback)
    var r = new XMLHttpRequest()
        // 设置请求方法和请求地址
    r.open(method, path, true)
        // 设置发送的数据的格式为 application/json
        // 这个不是必须的
    r.setRequestHeader('Content-Type', 'application/json')
        // 注册响应函数
    r.onreadystatechange = function() {
            if (r.readyState === 4) {
                // r.response 存的就是服务器发过来的放在 HTTP BODY 中的数据
                log('ajax response', r.response)
                var json = JSON.parse(r.response)
                responseCallback(json)
            }
        }
        // 把数据转换为 json 格式字符串
    data = JSON.stringify(data)
        // 发送请求
    r.send(data)
}